package View;

import Controller.Controller;
import Share.Constante.ConstanteIdModeServer;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author SpAdE
 */
public class View extends JFrame implements IView {

    private JFrame frm;
    private JPanel mainPanel;
    private JButton JBAnnualInstalment;
    private JButton JBYear;
    private JTextField JTFAmount;
    private JTextField JTFRate;
    private JTextField JTFYear;
    private JTextField JTFAnnualInstalment;
    private JButton JBCalculate;
    private JButton JBRestart;
    private ResourceBundle bundle;
    private Locale currentLocale;
    private ResourceBundle messages;
    private JMenuBar menuBar;
    private JMenu menu;
    private JMenuItem english, french;

    public View() {
        frm = null;
        mainPanel = null;
        JBAnnualInstalment = null;
        JBYear = null;
        JTFAmount = null;
        JTFRate = null;
        JTFYear = null;
        JTFAnnualInstalment = null;
        JBCalculate = null;
        JBRestart = null;
        bundle = null;
        currentLocale = null;
        messages = null;
        menuBar = null;
        menu = null;
        english = null;
        french = null;

    }

    @Override
    public void setView() {
        menuBar = new JMenuBar();
        menu = new JMenu("LANG");
        english = new JMenuItem("English");
        english.setActionCommand("english");
        french = new JMenuItem("Français");
        french.setActionCommand("french");
        menu.add(english);
        menu.add(french);
        menuBar.add(menu);


        frm = new JFrame();
        frm.setSize(500, 150);
        frm.setVisible(true);

        frm.setJMenuBar(menuBar);
        setLocal("en", "US");

        mainPanel = new JPanel();
        JBAnnualInstalment = new JButton();
        JBAnnualInstalment.setActionCommand("annualInstalment");

        JBYear = new JButton();
        JBYear.setActionCommand("year");

        JBCalculate = new JButton();
        JBCalculate.setActionCommand("calculate");

        JBRestart = new JButton();
        JBRestart.setActionCommand("restart");
    }

    @Override
    public void setMenu() {
        JPanel menuPanel = new JPanel();
        menuPanel.setLayout(new GridLayout(0, 1));

        JBYear.setText(messages.getString("BYear"));
        JBAnnualInstalment.setText(messages.getString("BAnnualI"));
        menuPanel.add(JBYear);
        menuPanel.add(JBAnnualInstalment);
        mainPanel.removeAll();
        mainPanel.add(menuPanel);
        frm.add(mainPanel, BorderLayout.NORTH);
        frm.setTitle(messages.getString("TMenu"));
        frm.repaint();
        frm.validate();

    }

    @Override
    public void setForm(int mode) {

        JPanel formPanel = new JPanel();
        formPanel.setLayout(new GridLayout(3, 3));
        JLabel JLAmount = new JLabel(messages.getString("LAmount"));
        JLabel JLRate = new JLabel(messages.getString("LRate"));
        JTFAmount = new JTextField(10);
        JTFRate = new JTextField(10);
        formPanel.add(JLAmount);
        formPanel.add(JLRate);
        if (mode == ConstanteIdModeServer.NOMBRE_ANNEE) {
            JLabel JLAnnualInstalment = new JLabel(messages.getString("LAnnualI"));
            formPanel.add(JLAnnualInstalment);
        }
        if (mode == ConstanteIdModeServer.ANNUITE) {
            JLabel JLYear = new JLabel(messages.getString("LYear"));
            formPanel.add(JLYear);
        }
        formPanel.add(JTFAmount);
        formPanel.add(JTFRate);
        if (mode == ConstanteIdModeServer.NOMBRE_ANNEE) {
            JTFAnnualInstalment = new JTextField(10);
            formPanel.add(JTFAnnualInstalment);
        }
        if (mode == ConstanteIdModeServer.ANNUITE) {
            JTFYear = new JTextField(10);
            formPanel.add(JTFYear);
        }
        JBCalculate.setText(messages.getString("BCalculate"));
        formPanel.add(JBCalculate);
        mainPanel.removeAll();
        mainPanel.add(formPanel);
        frm.setTitle(messages.getString("TForm"));
        frm.add(mainPanel, BorderLayout.NORTH);
        frm.repaint();
        frm.validate();
    }

    @Override
    public void displayResult(int mode, double result) {
        JPanel formPanel = new JPanel();
        formPanel.setLayout(new GridLayout(2, 2));

        JLabel JLResult;
        JLResult = new JLabel("");
        if (mode == ConstanteIdModeServer.NOMBRE_ANNEE) {
            JLResult = new JLabel(messages.getString("BYear") + " : " + result);
        }
        if (mode == ConstanteIdModeServer.ANNUITE) {
            JLResult = new JLabel(messages.getString("BAnnualI") + " : " + result);
        }

        formPanel.add(JLResult);
        JBRestart.setText(messages.getString("BMainMenu"));
        formPanel.add(JBRestart);
        mainPanel.removeAll();
        mainPanel.add(formPanel);
        frm.setTitle(messages.getString("TResult"));
        frm.add(mainPanel, BorderLayout.NORTH);
        frm.repaint();
        frm.validate();
    }

    @Override
    public void setActionListener(Controller cont) {
        JBAnnualInstalment.addActionListener(cont);
        JBYear.addActionListener(cont);
        JBCalculate.addActionListener(cont);
        JBRestart.addActionListener(cont);
        english.addActionListener(cont);
        french.addActionListener(cont);
        frm.addWindowListener(cont);
    }

    @Override
    public double getAmount() {
        double temp;
        if (!this.JTFAmount.getText().matches("")) {
            temp = Double.parseDouble(this.JTFAmount.getText());
            if (temp != -1) {
                return temp;
            }
        }
        return -1;
    }

    @Override
    public double getRate() {
        double temp;
        if (!this.JTFRate.getText().matches("")) {
            temp = Double.parseDouble(this.JTFRate.getText());
            if (temp != -1) {
                return temp;
            }
        }
        return -1;
    }

    @Override
    public double getYear() {
        double temp;
        if (!this.JTFYear.getText().matches("")) {
            temp = Double.parseDouble(this.JTFYear.getText());
            if (temp != -1) {
                return temp;
            }
        }
        return -1;
    }

    @Override
    public double getAnnualInstalment() {
        double temp;
        if (!this.JTFAnnualInstalment.getText().matches("")) {
            temp = Double.parseDouble(this.JTFAnnualInstalment.getText());
            if (temp != -1) {
                return temp;
            }
        }
        return -1;
    }

    @Override
    public void setLocal(String language, String country) {
        currentLocale = new Locale(language, country);
        messages = ResourceBundle.getBundle("Properties.messages", currentLocale);

    }

    @Override
    public void showError(int id) {
        String temp = messages.getString(id + "");
        if (temp != null) {
            JOptionPane.showMessageDialog(null, temp, "Erreur", JOptionPane.ERROR_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, "Erreur : Impossible de retrouver le message d'Erreur", "Erreur", JOptionPane.ERROR_MESSAGE);
        }
    }
}
